import { message } from 'antd';
import api from '../../services';
import { history } from 'umi';
const { followUser, queryFollowUser, queryUser, queryUserArticle,queryAllArticle } = api;

export default {
    namespace: 'attentionList',
    state: {
        attentionList: [],
        attentionUserInfo: [],
        userArticles: [],
        total: 0,
    },

    subscriptions: {
        setupHistory({ dispatch, history }) {  // eslint-disable-line
            return history.listen(({ pathname, state }) => {
                if (pathname === '/attentionList') {
                    dispatch({ type: "queryFollowUser" });
                    return;
                }
                if (pathname === '/attentionList/attentionDetail') {
                    dispatch({ type: "queryUser" });
                    dispatch({ type: "queryUserArticles", payload: { page: 1, pageSize: 10 } });
                }
            })
        },
    },

    effects: {
        * queryFollowUser({ }, { put, call, select }) {
            const userId = yield select(_ => _.user.userInfo.userId);
            const payload = { userId };
            const result = yield call(queryFollowUser, payload);
            if (result.code === 0) {
                yield put({ type: "updateState", payload: { attentionList: result.data } });
            }
            else {
                message.error(result.msg, 1);
            }
        },
        *followUser({ payload }, { put, call, select }) {
            const result = yield call(followUser, payload);
            if (result.code === 0) {
                message.success("取关成功");
                yield put({ type: "queryFollowUser" });
            }
            else {
                message.error(result.message, 1);
            }
        },
        *queryUser({ }, { put, call, select }) {
            const targetUserId = history.location.state?.targetUserId;
            const userId = yield select(_ => _.user.userInfo.userId);
            if (!targetUserId) return;
            const payload = { userId: userId || 0, targetUserId };
            const result = yield call(queryUser, payload);
            if (result.code === 0) {
                yield put({
                    type: "updateState", payload: { attentionUserInfo: result.data }
                })
            } else {
                message.error(result.message, 1);
            }
        },
        *queryUserArticles({ payload }, { put, call, select }) {
            const userId = yield select(_ => _.user.userInfo.userId);
            const targetUserId = history.location.state?.targetUserId;
            if (!targetUserId) return;
            const payloads = { userId, targetUserId, ...payload };
            const result = yield call(queryUserArticle, payloads);
            if (result.code === 0) {
                yield put({ type: "updateState", payload: { userArticles: result.data.records, total: result.data.total } });
            }
            else {
                message.error(result.message, 1);
            }
        },
    },
    reducers: {
        updateState(state, action) {
            return { ...state, ...action.payload };
        }
    }


}